package cn.szzsi.scanCode;

import cn.szzsi.common.model.Code;
import cn.szzsi.common.model.CodeTemplete;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;

/**
 * Created by Administrator on 2017/11/3 0003.
 */
public class ScanCodeService {
    public static final ScanCodeService me = new ScanCodeService();


    public String chooseCodeTemplete(String shopId, String scanCode) {
        //判断扫码使用哪个商品码模板
        String link_id = "";
        String short_url = "";
        CodeTemplete codeTemplete = new CodeTemplete();
        Record record = Db.findFirst("SELECT id, page, link_id FROM mall_code_templete WHERE shop_id=? AND `delete`=? AND status=? AND start_time<=NOW() AND end_time>=NOW()", shopId, 0, 0);
        if (record != null) {//当前时间有可使用的商品码模板
            codeTemplete = CodeTemplete.dao.findById(record.getStr("id"));
        } else {//当前时间没有有效的商品码模板,访问默认模板
            record = Db.findFirst("SELECT id, link_id FROM mall_code_templete WHERE shop_id=? AND `delete`=? AND status=? AND start_time IS NULL AND end_time IS NULL", shopId, 0, 0);
            codeTemplete = CodeTemplete.dao.findById(record.getStr("id"));
        }
        link_id = codeTemplete.getLinkId();
        Integer page = codeTemplete.getPage();
        switch (page) {
            case 0:
                break;
            case 1:
                break;
            case 2://绑定品效活动
                //根据渠道id查询渠道短链short_url
                break;
            case 3:
                break;
            case 4:
                break;
            default:
                break;
        }
        Code code = Code.dao.findById(scanCode);
        code.setScanType(page+1);
        code.setScanTime(new Date());
        code.update();
        return short_url;
    }
}
